package com.paishen.peiniwan;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;

/**
 * @description 活动生命控制器
 * @author wangpeng
 * @date 2014-12-20
 */
public class ActivityCollector {

	/** 活动列表容器 */
	public static List<Activity> activities = new ArrayList<>();

	public static void addActivity(Activity activity) {
		if (!activities.contains(activity)) {
			activities.add(activity);
		}
	}

	public static void removeActivity(Activity activity) {
		activities.remove(activity);
	}

	public static void finishActivity(Class<?> activityClazz) {
		for (Activity activity : activities) {
			if (activityClazz.equals(activity.getClass())) {
				activity.finish();
			}
		}
	}

	public static int size() {
		return activities.size();
	}

	public static Activity getTop() {
		if (activities.size() == 0) return null;
		return activities.get(activities.size() - 1);
	}

	public static Activity getActivity(Class<?> activityClazz) {
		for (Activity activity : activities) {
			if (activityClazz.equals(activity.getClass())) {
				return activity;
			}
		}
		return null;
	}

	public static void finishAllExcept(Class<?> activityClazz) {
		for (Activity activity : activities) {
			if (!activity.isFinishing()) {
				if (activity.getClass().getSimpleName().equals(activityClazz.getSimpleName())) {
					continue;
				}
				activity.finish();
			}
		}
	}

	public static void finishAll() {
		for (Activity activity : activities) {
			if (!activity.isFinishing()) activity.finish();
		}
	}

}
